package com.decent.text.pdf.test2.service;

import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 王雪杰
 * @date 2021/11/15 10:21
 */
@Service
public class ApiTestService {
    /**
     * 抠模板
     * todo Adobe Acrobat Pro DC 创建模板
     *
     * @throws Exception
     */
    public void createAllPdf(HttpServletResponse response) throws Exception {
        // 填充创建pdf
        PdfReader reader = null;
        PdfStamper stamp = null;
        try {
            reader = new PdfReader("D:\\idea\\workspace\\test-4-7\\src\\main\\resources\\pdfTest\\测试pdf模板.pdf");
            SimpleDateFormat simp = new SimpleDateFormat("dd-HH-mm-ss");
            String times = simp.format(new Date()).trim();
            // 创建生成报告名称
            String root = "D:\\idea\\workspace\\test-4-7\\src\\main\\resources\\pdfTest" + File.separator;
            File deskFile = new File(root, times + ".pdf");
            response.setContentType("application/pdf;charset=UTF-8");
            response.setHeader("Content-Disposition", "inline; filename=print.pdf");
            stamp = new PdfStamper(reader, response.getOutputStream());
            // 取出报表模板中的所有字段
            AcroFields form = stamp.getAcroFields();
            // 填充数据
            form.setField("seller_name", "zhangsan");
            form.setField("identify", "92837465748390123422");
            form.setField("mobile", "152746475659");

            form.setField("indate", "1");
            form.setField("start_year", "2021");
            form.setField("start_month", "11");
            form.setField("start_day", "15");
            form.setField("end_year", "2022");
            form.setField("end_month", "11");
            form.setField("end_day", "15");
            form.setField("fill_1", "lisi");
            form.setField("fill_2", "152746475659");
            form.setField("fill_3", "zhangsan");
            form.setField("fill_4", "152455555555@qq.com");
            form.setField("fill_5", "zhangsan");
            form.setField("fill_6", "152746475659");
            form.setField("fill_8", "2021");
            // // 遍历map装入数据
            // for (Entry<String, String> entry : map.entrySet()) {
            //     form.setField(entry.getKey(), entry.getValue());
            // }
            // 插入图片
            int pageNo = form.getFieldPositions("seller_sign_image").get(0).page;
            Rectangle signRect = form.getFieldPositions("seller_sign_image").get(0).position;
            float x = signRect.getLeft();
            float y = signRect.getBottom();
            Image image = Image.getInstance("D:\\idea\\workspace\\test-4-7\\src\\main\\resources\\pdfTest\\123.jpeg");
            // 获取操作的页面
            PdfContentByte under = stamp.getOverContent(pageNo);
            // 根据域的大小缩放图片
            image.scaleToFit(signRect.getWidth(), signRect.getHeight());
            // 添加图片
            image.setAbsolutePosition(x, y);
            under.addImage(image);

            // 这里true表示pdf可编辑
            stamp.setFormFlattening(true);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stamp != null) {
                stamp.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }
}
